<?php


namespace App\Services;

use App\Exceptions\Error;
use App\Models\AuthWx;
use App\Models\User;

class AuthWxService extends BaseService
{
    private $_repository;

    public function __construct()
    {
        parent::__construct();
        $this->_repository = repository()->AuthWxRepository;
    }

    /**
     * 新增授权信息
     * @param $params
     * @return \App\Models\AuthWx|\Illuminate\Database\Eloquent\Builder
     */
    public function createAuth($params)
    {
        return $this->_repository->create($params);
    }

    public function delAuthByUser($user)
    {
        if(empty($user->wechat)){
            return Error::handle();
        }

        $authWechat = $user->wechat;
        return $this->_repository->delAuth($authWechat);
    }

    /**
     * 通过unionid获取用户
     * @param $unionid
     * @return User|false|\Illuminate\Database\Eloquent\HigherOrderBuilderProxy|mixed
     */
    public function getUserInfoByUnionid($unionid)
    {
        return $this->_repository->getUserInfoByUnionid($unionid);
    }

    /**
     * 通过用户ID，获取授权信息
     *
     * @param $userId
     * @return AuthWx|bool [用户信息|false：没有次授权记录]
     */
    public function getAuthInfoByUserId($userId)
    {
        return $this->_repository->getAuthInfoByUserId($userId);
    }
}
